<?php
// 本类由系统自动生成，仅供测试用途
class HandlePostOrderAction extends Action {
	
	public function index(){

		$xml = simplexml_load_string($_POST['content']);//转换post数据为simplexml对象
		
		$i = 0;

		foreach($xml->children() as $child)//遍历所有节点数据
		{

			if ($child->getName() == 'order'){

				foreach ($child as $key => $order){

					if (!in_array($order->getName(),array('products_all','coupons_all'))) {

						//echo $order->getName() . ": " . $order . "<br />"; //打印节点名称和节点值

						$value = (array)$order;

						if ($order->getName() == 'order_time') {
							$neworder[$order->getName()] = strtotime(str_replace(':','',str_replace('-','',trim($value[0]))));
						}else {
							$neworder[$order->getName()] = $value[0];
						}

					}

				}

				$model = M('OrderInfo');

				$neworder['add_time'] = time();

				$order_id = $model->add($neworder);

				if ($order_id>0) {

					$order_no = $neworder['order_no'];
					$comm_no = $neworder['comm_no'];

					foreach ($child as $key => $order){

						if ($order->getName() == 'products_all') {
							foreach ($order as $k => $products){
								foreach ($products as $product){

									$value1 = (array)$product;

									$newproducts[$k]['order_id'] = $order_id;
									$newproducts[$k]['order_no'] = $order_no;
									$newproducts[$k]['comm_no'] = $comm_no;

									$newproducts[$k][$product->getName()] = $value1[0];
									//echo $product->getName() . ": " . $product . "<br />"; //打印节点名称和节点值
								}

							}
						}

						if ($order->getName() == 'coupons_all') {
							foreach ($order as $k => $coupons){
								foreach ($coupons as $coupon){
									$value2 = (array)$coupon;

									$newcoupons[$k]['order_id'] = $order_id;
									$newcoupons[$k]['order_no'] = $order_no;
									$newcoupons[$k]['comm_no'] = $comm_no;

									$newcoupons[$k][$coupon->getName()] = $value2[0];
									//echo $coupon->getName() . ": " . $coupon . "<br />"; //打印节点名称和节点值
								}

							}
						}

					}


					$OrderProduct = M('OrderProduct');

					foreach ($newproducts as $newproduct){
						$OrderProduct->add($newproduct);
					}


					$OrderCoupon = M('OrderCoupon');

					foreach ($newcoupons as $newcoupon){
						$OrderCoupon->add($newcoupon);
					}

					
				}


			}
			$i++;
		}
		

		$this->success('成功提交'.$i.'条订单!');
		//$this->redirect("ordersel/index");

	}
}